import sys
from uuid import UUID

import Ice,IceStorm,traceback

Ice.loadSlice("RuleTrigger.ice")
import xCyber

class HelloI(xCyber.RuleTrigger):
    def trigger(self,helloMsg,detailMsg):
        print("i receive msg "+helloMsg)


class Worker(Ice.Application):
    def run(self, args):
        topicName="111"
        #获取代理类
        topicManager=IceStorm.TopicManagerPrx.checkedCast(self.communicator().stringToProxy("IceStorm/TopicManager"))
        # 订阅
        try:
            topic=topicManager.retrieve(topicName)
        except IceStorm.NoSuchTopic as e:
            try:
                topic=topicManager.create(topicName)
            except IceStorm.TopicExists as ex:
                print("subscribe error")
                return
        adapter=self.communicator().createObjectAdapterWithEndpoints("pyAdapter", "tcp");
        identify=Ice.Identity()
        identify.id="DEMO"
        identify.name = Ice.generateUUID()
        subscriber=adapter.add(HelloI(),identify)
        subscriber =subscriber.ice_oneway()
        topic.subscribeAndGetPublisher({},subscriber)
        adapter.activate()
        self.communicator().waitForShutdown()


if __name__ == '__main__':
    app=Worker()
    sys.exit(app.main(sys.argv,"config.sub"))